package cool.qingqing.dataandtime;


import org.testng.annotations.Test;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author 高山青
 * @Date 2022/1/5  -   13:49
 */
public class DataDemo01 {
	/**
	 * jdk8日期类的更改
	 * 更改原因
	 */
	@Test
	public void test() {
		//Date设计不合理
		Date date=new Date(2021,12,11);
		//打印的时间不对
		System.out.println(date);
		//字符串转化成时间的时候线程不安全
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < 500; i++) {
			new Thread(()->{
				try {
					System.out.println(dateFormat.parse("2021-12-11"));
				}catch (Exception e){
					System.out.println(e);
				}

			}).start();
		}


	}

	/**
	 * 新的时间
	 */
	@Test
	public void test01() {
		//使用方式
	LocalDate localDate=LocalDate.of(2021,12,11);
		System.out.println(localDate);

		//获取当前日期
		LocalDate now = LocalDate.now();
		System.out.println(now);

		System.out.println(now.getYear());
		System.out.println(now.getMonth().getValue());
		System.out.println(now.getDayOfMonth());
		System.out.println(now.getDayOfWeek().getValue());
	}

	/**
	 * 时间类型
	 */
	@Test
	public void test02() {
		LocalTime localTime = LocalTime.of(4, 3, 33, 233);
		System.out.println(localTime);

		LocalTime now = LocalTime.now();
		System.out.println(now);

		System.out.println(now.getHour());
		System.out.println(now.getMinute());
		System.out.println(now.getSecond());
		System.out.println(now.getNano());
	}

	/**
	 * 日期时间类型
	 */
	@Test
	public void test03() {
		LocalDateTime l = LocalDateTime.of(2021, 11, 12, 4, 5, 23, 123);
		System.out.println(l);
	}

	/**
	 * 格式化和解析操作
	 */
	@Test
	public void test04() {
		LocalDateTime l = LocalDateTime.of(2021, 11, 12, 4, 5, 23, 123);
		System.out.println(l);
		DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		String format = l.format(isoLocalDateTime);
		System.out.println(format);

		//h只能是大写
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String format1 = l.format(dateTimeFormatter);
		System.out.println(format1);

		LocalDateTime localDateTime=LocalDateTime.parse("2011-11-22 04:05:23",dateTimeFormatter);
		System.out.println(localDateTime);


	}
}
